#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct student {
    string name;
    int y, m, d;
    int t;
    student(string s, int a, int b, int c, int x) : name(s), y(a), m(b), d(c), t(x) {}
};
bool compare(student& s1, student& s2) {
    if(s1.y != s2.y) return s1.y < s2.y;
    if(s1.m != s2.m) return s1.m < s2.m;
    if(s1.d != s2.d) return s1.d < s2.d;
    return s1.t > s2.t;
}
int main() {
    int n;
    cin >> n;
    vector<student> v;
    for(int i = 0; i < n; i++)
    {
        string s;
        int x, y, z;
        cin >> s >> x >> y >> z;
        v.push_back(student(s, x, y, z, i));
    }
    sort(v.begin(), v.end(), compare);
    for(auto& s : v) {
        cout << s.name << endl;
    }
    return 0;
}